﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eTruck.Helpers;

namespace eTruck.Views.Supplier
{
    public partial class Form : System.Web.UI.Page
    {
        protected string returnValue = string.Empty;

        BLL.Supplier bll = new BLL.Supplier();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = System.Web.HttpUtility.UrlDecode(Request["Type"]);
            if (type == "Save")
            {
                Save();
            }
            else
            {
                Exists();
            }
        }

        private void Save()
        {
            Model.Supplier model = new Model.Supplier();
          

            string CorporationID = System.Web.HttpUtility.UrlDecode(Request["CorporationID"]);
            model.CorporationName = System.Web.HttpUtility.UrlDecode(Request["CorporationName"]);

            model.Address = System.Web.HttpUtility.UrlDecode(Request["Addressstr"]);
            
            model.Contact = System.Web.HttpUtility.UrlDecode(Request["Contact"]);
            model.Phone=System.Web.HttpUtility.UrlDecode(Request["Phone"]);
            model.LastUpdated = DateTime.Now;

            try
            {
                model.UserID = Convert.ToInt32(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
            }
            catch { }

            try
            {
                bool result = false;
                if (CorporationID.Length == 0)
                {
                    //create new User
                    result = bll.Add(model);
                }
                else
                {
                    //update User Info...
                    model.CorporationID = Convert.ToInt32(CorporationID);
                    result = bll.Update(model);
                }
                if (result)
                {
                    returnValue = "True";
                }
                else
                {
                    returnValue = "False";
                }
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
        }

        private void Exists()
        {
            string CorporationName = System.Web.HttpUtility.UrlDecode(Request["CorporationName"]);

            DataSet ds = bll.GetList("CorporationName='" + CorporationName + "'");
            if (ds == null)
            {
                returnValue = "False";
            }
            else if (ds.Tables.Count == 0)
            {
                returnValue = "False";
            }
            else if (ds.Tables[0].Rows.Count == 0)
            {
                returnValue = "False";
            }
            else
            {
                returnValue = "True";
            }

        }
    }
}

